package com.mmy.service.impl.restaurant;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mmy.controller.Common;
import com.mmy.dao.restaurant.TRestaurantOrderRepository;
import com.mmy.domain.restaurant.TRestaurantOrder;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.restaurant.RestaurantOrderService;

@Service
public class RestaurantOrderServiceImpl implements RestaurantOrderService {

	@Autowired
	TRestaurantOrderRepository tRestaurantOrderRepository;

	/**
	 * 个人中心通过登录人获取餐馆预留 获取分页
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public HashMap<String, Integer> mapRestaurantReserve(String pageNumber, String pageSize, int iUserID) {
		Common common = new Common();
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		mapPage = common.getPage(pageNumber, pageSize, tRestaurantOrderRepository.countByUserID(iUserID));
		return mapPage;
	}

	/**
	 * 个人中心通过登录人获取餐馆退单 获取分页
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public HashMap<String, Integer> mapRestaurantRefund(String pageNumber, String pageSize, int iUserID) {
		Common common = new Common();
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		mapPage = common.getPage(pageNumber, pageSize, tRestaurantOrderRepository.countByRefundUserID(iUserID));
		return mapPage;
	}

	/**
	 * 个人中心通过登录人获取餐馆订单 获取分页
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public HashMap<String, Integer> mapRestaurantOrder(String pageNumber, String pageSize, int iUserID) {
		Common common = new Common();
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		mapPage = common.getPage(pageNumber, pageSize, tRestaurantOrderRepository.countByOrderUserID(iUserID));
		return mapPage;
	}

	/**
	 * 个人中心通过登录人获取餐馆预留
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public List<TRestaurantOrder> findByUserID(String pageNumber, String pageSize, int iUserID) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		List<TRestaurantOrder> lstRestaurantOrder = new ArrayList<TRestaurantOrder>();
		lstRestaurantOrder = tRestaurantOrderRepository.findByUserID(iUserID, iTempPageNo, iPageSize);
		return lstRestaurantOrder;
	}

	/**
	 * 个人中心通过登录人获取餐馆订单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public List<TRestaurantOrder> findByOrderUserID(String pageNumber, String pageSize, int iUserID) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		List<TRestaurantOrder> lstRestaurantOrder = new ArrayList<TRestaurantOrder>();
		lstRestaurantOrder = tRestaurantOrderRepository.findByOrderUserID(iUserID, iTempPageNo, iPageSize);
		return lstRestaurantOrder;
	}

	/**
	 * 个人中心通过登录人获取餐馆退单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public List<TRestaurantOrder> findByChargeBackUserID(String pageNumber, String pageSize, int iUserID) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		List<TRestaurantOrder> lstRestaurantOrder = new ArrayList<TRestaurantOrder>();
		lstRestaurantOrder = tRestaurantOrderRepository.findByChargeBackUserID(iUserID, iTempPageNo, iPageSize);
		return lstRestaurantOrder;
	}

	/**
	 * 个人中心通过登录人取消餐馆预留
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public void updateState(int iID, TSysUser sessionSysUser) {
		// TODO Auto-generated method stub
		TRestaurantOrder dbRestaurantOrder = tRestaurantOrderRepository.findByID(iID);
		dbRestaurantOrder.setOrderState((byte) 3);
		dbRestaurantOrder.setModifyDate(new Date());
		dbRestaurantOrder.setReviser(sessionSysUser.getUserName());
		tRestaurantOrderRepository.save(dbRestaurantOrder);
	}

	/**
	 * 个人中心通过登录人申请餐馆退单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public void updateRefundState(int iID, TSysUser sessionSysUser) {
		// TODO Auto-generated method stub
		TRestaurantOrder dbRestaurantOrder = tRestaurantOrderRepository.findByID(iID);
		dbRestaurantOrder.setOrderState((byte) 4);
		dbRestaurantOrder.setRefundProcess((byte) 1);
		dbRestaurantOrder.setModifyDate(new Date());
		dbRestaurantOrder.setReviser(sessionSysUser.getUserName());
		tRestaurantOrderRepository.save(dbRestaurantOrder);
	}

	/**
	 * 个人中心通过登录人点击撤销线路退单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public void updateCancelState(int iID, TSysUser sessionSysUser) {
		// TODO Auto-generated method stub
		TRestaurantOrder dbRestaurantOrder = tRestaurantOrderRepository.findByID(iID);
		dbRestaurantOrder.setOrderState((byte) 2);
		dbRestaurantOrder.setRefundProcess((byte) 0);
		dbRestaurantOrder.setModifyDate(new Date());
		dbRestaurantOrder.setReviser(sessionSysUser.getUserName());
		tRestaurantOrderRepository.save(dbRestaurantOrder);
	}

	/**
	 * 通过订单ID查询
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public TRestaurantOrder findByID(int iID) {
		// TODO Auto-generated method stub
		return tRestaurantOrderRepository.findByID(iID);
	}

}
